//
//  EditSignatureViewController.m
//  安好
//
//  Created by 马晓良 on 14-11-12.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "EditSignatureViewController.h"
#import "XHDrawerController.h"
@interface EditSignatureViewController ()<UITextViewDelegate>
/**
 *  实现签名编辑
 */
@property (strong, nonatomic) IBOutlet UITextView *signatureTextView;


@property (strong, nonatomic) IBOutlet UILabel *placeholderlable;

/**
 *  要上传的数据存在此字典中
 */
@property (nonatomic, strong) NSMutableDictionary *uploadInfoDic;
@property (nonatomic, strong) UIButton *rightButton;
@end

@implementation EditSignatureViewController

- (NSMutableDictionary *)uploadInfoDic{
    if (_uploadInfoDic == nil) {
        self.uploadInfoDic = [NSMutableDictionary dictionary];
    }
    return _uploadInfoDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.navigationItem.title = @"个性签名";
    self.signatureTextView.delegate = self;
    [self setNaviBarAttribute];
    
    if (self.originalSignature.length < 1) {
    }else{
        self.placeholderlable.hidden = YES;
        self.signatureTextView.text = self.originalSignature;
    }
    self.placeholderlable.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
    self.signatureTextView.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.signatureTextView becomeFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// block 传值
- (void)getTextViewInfo:(backPersonalDataSignatureInfo)string{
    self.textViewText = string;
}


#pragma mark - 设置naviBar 并实现左右naviBar button方法 -
- (void)setNaviBarAttribute{
    /**
     *  设置左NaviBar和右naviBar
     */
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton.frame = CGRectMake(0, 0, 23, 23);
    [self.rightButton setImage:[UIImage imageNamed:@"公用_完成"] forState:UIControlStateNormal];
    [self.rightButton addTarget:self action:@selector(handleRightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
}

/**
 *  返回
 */
- (void)handleLeftButtonAction{
    if (self.returnWhichVC) {
        [self.drawerController closeDrawerAnimated:YES completion:nil];
        [self.navigationController popToRootViewControllerAnimated:YES];
        self.returnWhichVC = NO;
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/**
 *  完成(返回并传值)
 */
- (void)handleRightButtonAction{
    if (self.returnWhichVC) {
        [self.uploadInfoDic setValue:self.signatureTextView.text forKey:@"bio"];
        [self _parsePersonalDataSource:kBaseURL];
        self.returnWhichVC = NO;
    }else {
        [self.uploadInfoDic setValue:self.signatureTextView.text forKey:@"bio"];
        [self _parsePersonalDataSource:kBaseURL];
    }
}

#pragma mark - 网路数据上传 -
- (void)_parsePersonalDataSource:(NSString *)urlString{
    // 获取url地址
    NSString * postURL = [NSString stringWithFormat:@"%@user/changeuser/",urlString];
    [self.rightButton removeTarget:self action:@selector(handleRightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [self.uploadInfoDic setValue:kPlatform forKey:@"platform"];
    [self.uploadInfoDic setValue:kVersion forKey:@"version"];
    [manager POST:postURL parameters:self.uploadInfoDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            [self showCustomHudSingleLine:@"上传失败"];
            [self.rightButton addTarget:self action:@selector(handleRightButtonAction) forControlEvents:UIControlEventTouchUpInside];
        }else{
            MyNSLog("========%@========", [responseObject objectForKey:@"msg"]);
            if (self.returnWhichVC) {
                [self.drawerController closeDrawerAnimated:YES completion:nil];
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
//                self.textViewText(self.signatureTextView.text);
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self showCustomHudSingleLine:@"上传失败"];
        [self performSelector:@selector(returnPopVC) withObject:nil afterDelay:1];
    }];
}

- (void)returnPopVC{
    if (self.returnWhichVC) {
        [self.drawerController closeDrawerAnimated:YES completion:nil];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - textField的代理方法 -

-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length>0) {
        self.placeholderlable.hidden = YES;
    }else
    {
        self.placeholderlable.hidden = NO;
    }
    
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    [self.signatureTextView resignFirstResponder];
    
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }else
    {
        return YES;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
